﻿using System.Collections;
using System.Collections.Generic;
using TBFramework;
using UnityEngine;

public abstract class RunClass : BaseClass
{
    public abstract void SetTransform();
    public abstract void ResetTransforms();

}
/// <summary>
/// 物体变换类 如一个物体的所有子物体归位
/// </summary>
public class SetObjectTransform : RunClass
{
    Transform target;
    List<Transform> transforms = new List<Transform>();
    List<Vector3> position = new List<Vector3>();
    List<Quaternion> rotate = new List<Quaternion>();
    List<Vector3> scale = new List<Vector3>();

    bool isResetAnimation = false;
    bool isResetMaterial = false;
    Dictionary<Transform,Material[]> transMaterials = new Dictionary<Transform, Material[]>(); 
    Dictionary<Transform,Animation> transAnimation = new Dictionary<Transform, Animation>(); 
    /// <summary>
    /// 物体变换类 如一个物体的所有子物体归位
    /// transform 父级物体
    /// isResetAnimation 是否重置动画
    /// isResetMaterial 是否重置材质
    /// </summary>
    /// <param name="param"></param>
    public SetObjectTransform((Transform transform, bool isResetAnimation, bool isResetMaterial) param)
    {
        isResetAnimation = param.isResetAnimation;
        isResetMaterial = param.isResetMaterial;
        target = param.transform;
        SetTransform();
    }
    public override void SetTransform()
    {
        transforms.Add(target);
        position.Add(target.position);
        rotate.Add(target.rotation);
        scale.Add(target.localScale);

        if (isResetMaterial)
        { 
            var renderer = target.GetComponent<Renderer>();
            transMaterials.Add(target, renderer.materials);
        }
        // if (isResetAnimation)
        // {
        //     transAnimation.Add(target,target.GetComponent<Animation>());            
        // }
        TraverseChildren(target);
    }
    void TraverseChildren(Transform parent)
    {
        foreach (Transform child in parent)
        {
            transforms.Add(child);
            position.Add(child.position);
            rotate.Add(child.rotation);
            scale.Add(child.localScale);
            if (isResetMaterial)
            { 
                var renderer = child.GetComponent<Renderer>();
                transMaterials.Add(child, renderer.materials);
            }
            // if (isResetAnimation)
            // {
            //     transAnimation.Add(child,child.GetComponent<Animation>());            
            // }
            TraverseChildren(child);
        }
    }
    public override void ResetTransforms()
    {
        for (int i = 0; i < transforms.Count; i++)
        {
            transforms[i].position = position[i];
            transforms[i].rotation = rotate[i];
            transforms[i].localScale = scale[i];
            if (isResetMaterial)
            {
                transforms[i].GetComponent<Renderer>().materials = transMaterials[transforms[i]];
            }
            if (isResetAnimation)
            {
                Animation animation = transforms[i].GetComponent<Animation>();
                if (animation != null && animation.clip != null)
                {
                    animation[animation.clip.name].time = 0;
                    animation.Sample();
                }
               // transforms[i].GetComponent<Animation>().Play(animation.clip.name);                
            }
        }
    }
}



